﻿namespace OleDbDAL.Stock
{
    using DBUtility;
    using IDal.Stock;
    using Model.Stock;
    using System;
    using System.Data;
    using System.Text;

    public class StockWarningApp : IStockWarning
    {
        private string dbname = "Systems";

        public void Add(StockWarningEnt model)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("insert into T_StockWarning(");
            builder.Append("StockId,ChainNum,CommId,CommName,MinStock,MaxStock,WarTtitle,WarText,ProcurementNum,WarType,WarMan,WarTime");
            builder.Append(")");
            builder.Append(" values (");
            builder.Append("" + model.StockId + ",");
            builder.Append("" + model.ChainNum + ",");
            builder.Append("" + model.CommId + ",");
            builder.Append("'" + model.CommName + "',");
            builder.Append("" + model.MinStock + ",");
            builder.Append("" + model.MaxStock + ",");
            builder.Append("'" + model.WarTtitle + "',");
            builder.Append("'" + model.WarText + "',");
            builder.Append("" + model.ProcurementNum + ",");
            builder.Append("" + model.WarType + ",");
            builder.Append("'" + model.WarMan + "',");
            builder.Append("'" + model.WarTime + "'");
            builder.Append(")");
            DbHelperOledb.ExecuteSql(this.dbname, builder.ToString());
        }

        public void Delete(int WarId)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("delete from T_StockWarning ");
            builder.Append(" where WarId=" + WarId);
            DbHelperOledb.ExecuteSql(this.dbname, builder.ToString());
        }

        public DataTable GetList(string strWhere)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select * from T_StockWarning ");
            if (strWhere.Trim() != "")
            {
                builder.Append(" where " + strWhere);
            }
            builder.Append(" order by WarId ");
            return DbHelperOledb.Query(this.dbname, builder.ToString());
        }

        public int GetMaxId(string swhere)
        {
            return DbHelperOledb.GetMaxID(this.dbname, "WarId", "T_StockWarning", swhere);
        }

        public StockWarningEnt GetModel(int WarId)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select WarId,StockId,ChainNum,CommId,CommName,MinStock,MaxStock,WarTtitle,WarText,ProcurementNum,WarType,WarMan,WarTime from T_StockWarning ");
            builder.Append(" where WarId=" + WarId);
            StockWarningEnt ent = new StockWarningEnt();
            DataTable table = DbHelperOledb.Query(this.dbname, builder.ToString());
            ent.WarId = WarId;
            if (table.Rows.Count > 0)
            {
                if (table.Rows[0]["StockId"].ToString() != "")
                {
                    ent.StockId = int.Parse(table.Rows[0]["StockId"].ToString());
                }
                if (table.Rows[0]["ChainNum"].ToString() != "")
                {
                    ent.ChainNum = int.Parse(table.Rows[0]["ChainNum"].ToString());
                }
                if (table.Rows[0]["CommId"].ToString() != "")
                {
                    ent.CommId = int.Parse(table.Rows[0]["CommId"].ToString());
                }
                ent.CommName = table.Rows[0]["CommName"].ToString();
                if (table.Rows[0]["MinStock"].ToString() != "")
                {
                    ent.MinStock = int.Parse(table.Rows[0]["MinStock"].ToString());
                }
                if (table.Rows[0]["MaxStock"].ToString() != "")
                {
                    ent.MaxStock = int.Parse(table.Rows[0]["MaxStock"].ToString());
                }
                ent.WarTtitle = table.Rows[0]["WarTtitle"].ToString();
                ent.WarText = table.Rows[0]["WarText"].ToString();
                if (table.Rows[0]["ProcurementNum"].ToString() != "")
                {
                    ent.ProcurementNum = int.Parse(table.Rows[0]["ProcurementNum"].ToString());
                }
                if (table.Rows[0]["WarType"].ToString() != "")
                {
                    ent.WarType = int.Parse(table.Rows[0]["WarType"].ToString());
                }
                ent.WarMan = table.Rows[0]["WarMan"].ToString();
                if (table.Rows[0]["WarTime"].ToString() != "")
                {
                    ent.WarTime = DateTime.Parse(table.Rows[0]["WarTime"].ToString());
                }
                return ent;
            }
            return null;
        }

        public DataTable GetPageList(int PageSize, int PageIndex, string strWhere)
        {
            string orderString = "";
            int pageCount = 0;
            int recordCount = 0;
            StringBuilder builder = new StringBuilder();
            builder.Append("select * from T_StockWarning ");
            return DbHelperOledb.ExecutePager(this.dbname, PageIndex, PageSize, "", builder.ToString(), strWhere, orderString, out pageCount, out recordCount, "WarId");
        }

        public void Update(StockWarningEnt model)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("update T_StockWarning set ");
            builder.Append("StockId=" + model.StockId + ",");
            builder.Append("ChainNum=" + model.ChainNum + ",");
            builder.Append("CommId=" + model.CommId + ",");
            builder.Append("CommName='" + model.CommName + "',");
            builder.Append("MinStock=" + model.MinStock + ",");
            builder.Append("MaxStock=" + model.MaxStock + ",");
            builder.Append("WarTtitle='" + model.WarTtitle + "',");
            builder.Append("WarText='" + model.WarText + "',");
            builder.Append("ProcurementNum=" + model.ProcurementNum + ",");
            builder.Append("WarType=" + model.WarType + ",");
            builder.Append("WarMan='" + model.WarMan + "',");
            builder.Append("WarTime='" + model.WarTime + "'");
            builder.Append(" where WarId=" + model.WarId + "");
            DbHelperOledb.ExecuteSql(this.dbname, builder.ToString());
        }
    }
}

